﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Media;
using System.Threading;
using BOMS.Utility;
using System.Text;

namespace BOMS.WebUI.Web.Warning
{
    public partial class WarningPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Repeater1.ItemDataBound += new RepeaterItemEventHandler(Repeater1_ItemDataBound);
            if (!IsPostBack)
            {
                Repeater1Bind();
            }
        }

        void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem
                || e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.EditItem)
            {
                Model.S_Warning md = e.Item.DataItem as Model.S_Warning;
                //电池ID号
                Literal lit = e.Item.FindControl("ltrBatteryID") as Literal;
                if (lit != null)
                {
                    lit.Text = (md.BatteryID == -1 ? "[整组]" : md.BatteryID.ToString());
                }
                //显示报警信息、图片
                Literal ltrWarningDesc = e.Item.FindControl("ltrWarningDesc") as Literal;
                if (ltrWarningDesc != null)
                {
                    StringBuilder sb = new StringBuilder();

                    //电池组
                    if (md.BatteryID == -1)
                    {
                        ltrWarningDesc.Text = Common.BuildWarningString<GroupErrorStatus>(md.WarningNO,true);
                    }
                    else//电池
                    {
                        ltrWarningDesc.Text = Common.BuildWarningString<BatteryErrorStatus>(md.WarningNO,true);
                    }
                    
                }
              
            }
        }

        private void Repeater1Bind()
        {
            BLL.S_Warning bll = new BLL.S_Warning();
            List<Model.S_Warning> list = bll.GetModelList("IsActive=1");
            Repeater1.DataSource = list;
            Repeater1.DataBind();
            //只要有数据就报警
            if (list.Count > 0)
            {
                AlarmWarning();

            }
        }

        private void AlarmWarning()
        {

            string hostName=Request.Url.Host;
            string port = Request.Url.Port.ToString();
            string url = hostName + ":" + port;
            // Response.Write(url);
            Type cstype = this.GetType();
            ClientScript.RegisterStartupScript(cstype, "key", "<script>play_click(this,'http://" + url + "/Alarm/music/alarm.wav');</script>");
        }
    }
}